package com.wxd.framework.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wxd.framework.common.FrameConstants;
import com.wxd.framework.common.Criteria;
import com.wxd.framework.common.page.Pagination;
import com.wxd.framework.dao.RoleDao;
import com.wxd.framework.dao.UserDao;
import com.wxd.framework.dao.UserRoleDao;
import com.wxd.framework.entity.Role;
import com.wxd.framework.entity.User;
import com.wxd.framework.entity.UserRole;
import com.wxd.framework.service.UserService;
import com.wxd.framework.vo.TreeItem;

@Service
public class UserServiceImpl extends BaseServiceImpl implements UserService{

	@Autowired
	private UserDao userDao;
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private UserRoleDao userRoleDao;
	
	/* (non-Javadoc)
	 * @see com.wxd.framework.service.UserService#stopOneUser(java.lang.String, boolean, java.lang.String)
	 */
	@Override
	public void stopOneUser(String id, boolean isStoped, String updater) {
		User user = new User();
		user.setId(id);
		user.setIsStoped(!isStoped);
		user.setUpdater(updater);
		user.setUpdateTime(new Date());
		userDao.updateSelective(user);
	}

	/* (non-Javadoc)
	 * @see com.wxd.framework.service.UserService#getRoleTreeByUserId(java.lang.String)
	 */
	@Override
	public List<TreeItem> getRoleTreeByUserId(String userId) {
		List<TreeItem> tree = new ArrayList<>();
		List<Role> roles = roleDao.selectAllListUnStoped();
		List<UserRole> userRoles = userRoleDao.selectByUserId(userId);
		for(Role role :roles){
			TreeItem item = new TreeItem(role);
			for(UserRole userRole : userRoles){
				if (userRole.getRoleId().equals(item.getId())) {
					item.setChecked(true);
					break;
				}
			}
			tree.add(item);
		}
		return tree;
	}

	/* (non-Javadoc)
	 * @see com.wxd.framework.service.UserService#saveUserRoleForUser(java.lang.String, java.lang.String)
	 */
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public void saveUserRoleForUser(String userId, String roleIds) {
		//清空该用户的所有角色关系
		userRoleDao.deleteByUserId(userId);
		String[] roleIdArray = roleIds.split(FrameConstants.NORMAL_FLAG);
		for(String roleId : roleIdArray){
			if(StringUtils.isNotBlank(roleId)){
				UserRole userRole = new UserRole();
				userRole.setUserId(userId);
				userRole.setRoleId(roleId);
				userRoleDao.insertSelective(userRole);
			}
		}
	}

	@Override
	public User addOneUser(User user) {
		List<User> list = userDao.selectByUserName(user.getUserName());
		if (list != null && list.size() > 0) {
			user.setMessage("用户已存在");
		}else{
			userDao.insertSelective(user);
		}		
		return user;
	}

	@Override
	public void deleteOneUser(String id) {
		userDao.deleteByPrimaryKey(id);
	}

	@Override
	public void editOneUser(User user) {
		userDao.updateSelective(user);
	}

	@Override
	public User getOneUserById(String id) {
		return userDao.selectByPrimaryKey(id);
	}

	@Override
	public Pagination getUsersInPage(Criteria criteria) {
		return userDao.getPaginationByCriteria(criteria);
	}

}
